//
//  customPageControl.m
//  HappyStore
//
//  Created by 点点充 on 16/1/15.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "customPageControl.h"

@implementation customPageControl

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setBackgroundColor:[UIColor clearColor]];
        //笔刷宽度
        self.iStrokeWidth = 10;
        //页码之间间隙
        self.iGapWidth = 25;
        //直径
        self.iDiameter = 12;
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapped:)];
        [self addGestureRecognizer:tapGestureRecognizer];
    }
    return self;
}

- (void)onTapped:(UITapGestureRecognizer*)gesture
{
    CGPoint touchPoint = [gesture locationInView:[gesture view]];
    if (touchPoint.x < self.frame.size.width/2)
    {
        // move left
        if (self.currentPage>0)
        {
            self.currentPage -= 1;
        }
    }
    else
    {
        // move right
        if (self.currentPage<self.numberOfPages-1)
        {
            self.currentPage += 1;
        }
    }
    [self setNeedsDisplay];
    
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showNextPage" object:nil];
    
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context, self.iStrokeWidth);
    
    int gap = self.iGapWidth;
    
    float _diameter = self.iDiameter - 2*self.iStrokeWidth;
    
    int total_width = self.numberOfPages*_diameter + (self.numberOfPages-1)*gap;
    
    //5*8+4*10 80
    
    if (total_width>self.frame.size.width)
    {
        while (total_width>self.frame.size.width)
        {
            _diameter -= 2;
            
            gap = _diameter + 2;
            
            while (total_width>self.frame.size.width)
                
            {
                
                gap -= 1;
                
                total_width = self.numberOfPages*_diameter + (self.numberOfPages-1)*gap;
                
                if (gap==2)
                    
                {
                    break;
                    total_width = self.numberOfPages*_diameter + (self.numberOfPages-1)*gap;
                    
                }
                
            }
            if (_diameter==2)
                
            {
                break;
                
                total_width = self.numberOfPages*_diameter + (self.numberOfPages-1)*gap;
            }
            
        }
        
    }
    
    for (int i=0; i<self.numberOfPages; i++)
        
    {
        int _currentPageDiameter = self.iDiameter;
        int x = (self.frame.size.width-total_width)/2 + i*(_diameter+gap) - (_currentPageDiameter-_diameter)/2;
        
        if (i==self.currentPage)
        {
            CGContextSetRGBFillColor(context, 32 / 255.0, 165 / 255.0, 240 / 255.0, 1.0);
            
            CGContextFillRect(context, CGRectMake(x,(self.frame.size.height-_currentPageDiameter)/2,_currentPageDiameter,_currentPageDiameter/3));
        }
        else
        {
            CGContextSetRGBFillColor(context, 255.0 / 255.0, 255.0 / 255.0, 255.0 / 255.0, 0.6);
            
            CGContextFillRect(context, CGRectMake(x,(self.frame.size.height-_currentPageDiameter)/2,_currentPageDiameter,_currentPageDiameter/3));
        }
        
    }
}

@end
